package com.web.controller.system;

import com.domain.system.Module;
import com.github.pagehelper.PageInfo;
import com.service.system.ModuleService;
import com.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){
        PageInfo pageInfo =  moduleService.findAll(page, size);
        request.setAttribute("page", pageInfo);
        return "system/module/module-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        List<Module> deptList = moduleService.findAll();
        request.setAttribute("menus", deptList);
        return "system/module/module-add";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Module module = moduleService.findById(id);
        List<Module> deptList = moduleService.findAll();
        request.setAttribute("deptList", deptList);
        request.setAttribute("module", module);
        return "system/module/module-update";
    }

    @RequestMapping("/edit")
    public String toAdd(Module module){
        if (StringUtils.isBlank(module.getId())) {
            Integer result = moduleService.save(module);
        }else{
            Integer result = moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id){
        Integer result = moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }

}
